package com.sinoglobal.sinostore.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.lidroid.xutils.util.LogUtils;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.bean.ShopWareCsVo;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.ApiDebugUtil;
/**
 * 
* @title: GoodsDescFragment.java 
* @package com.chiliring.sinostore.fragment 
* @description: TODO(图文详情) 
* @author LK
* @date 2015-1-23 下午3:23:44
 */
public final class GoodsDescFragment extends Fragment {
	private int type=1;//1厂商 2 商家
	private String id;
	
	/**没有详情数据**/
	private TextView tvNodetail;
	
	private LinearLayout llGoodsInfo;
	
	public static GoodsDescFragment newInstance(int type,String id) {
		GoodsDescFragment fragment = new GoodsDescFragment();
		fragment.type = type;
		fragment.id = id;
		return fragment;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		LogUtils.e("onCreateView");
		View layout=inflater.inflate(R.layout.shop_fragment_goods_info, null);
		initView(layout);
		initData();
		return layout;
	}
	
	private void initView(View view){
		tvNodetail = (TextView)view.findViewById(R.id.tv_nodetail);
		llGoodsInfo = (LinearLayout)view.findViewById(R.id.goods_info);
	}
	
	private void initData(){
		final ProgressDialog dialog = new ProgressDialog(getActivity());
		try {
			dialog.show();
		} catch (Exception e) {
		}
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("por","103");
		params.addQueryStringParameter("proIden",Constants.STOREFLAG);
		params.addQueryStringParameter("id",id);
		params.addQueryStringParameter("type",String.valueOf(type));
		ApiDebugUtil.debug(params);
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.send(HttpMethod.POST, Constants.STORE_URL, params, new RequestCallBack<String>() {
			@Override
			public void onStart() {
				super.onStart();
				LogUtils.i("jjjjjjjj="+this.getRequestUrl());
			}

			@Override
			public void onFailure(HttpException arg0, String arg1) {
				try {
					dialog.dismiss();
				} catch (Exception e) {
				}
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				try {
					dialog.dismiss();
				} catch (Exception e) {
				}
				String result = responseInfo.result;
				LogUtils.e("resultresultresultresult="+result);
				if(!TextUtils.isEmpty(result)){
					ShopWareCsVo entity;
					try {
						entity = JSON.parseObject(result, ShopWareCsVo.class);
					} catch (Exception e) {
						e.printStackTrace();
						Toast.makeText(getActivity(), "数据解析出错", 0).show();
						return;
					}
					if(Constants.STORE_SUCCESS_CODE.equals(entity.getCode())){
						WebView view = new WebView(getActivity());
						view.loadDataWithBaseURL("about:blank", entity.getDetails(), "text/html", "utf-8", null);
						llGoodsInfo.addView(view);
						llGoodsInfo.setVisibility(View.VISIBLE);
					}else{
						llGoodsInfo.setVisibility(View.GONE);
						tvNodetail.setVisibility(View.VISIBLE);
						tvNodetail.setText(entity.getMessage());
					}
				}
			}
		});
	}
	
}
